package com.rp.system.user.filter;//package com.rp.system.user.filter;
//
//import com.alibaba.druid.support.http.WebStatFilter;
//import com.fasterxml.jackson.core.filter.TokenFilter;
//import ResultDTO;
//import IPUtils;
//import RedisUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.shiro.web.util.WebUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.web.servlet.FilterRegistrationBean;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.annotation.Bean;
//import org.springframework.core.annotation.Order;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.support.WebApplicationContextUtils;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.*;
//import java.util.concurrent.TimeUnit;
//
///**
// * 类说明:
// *
// * @author ****
// * @date 2020-12-21 15:24  新建
// */
//
//@Slf4j
//@Order(0)
//@Component
//@WebFilter(filterName = "IPFilter", urlPatterns = "/*")
//public class IPFilter implements Filter {
//
//    @Bean
//    FilterRegistrationBean tokenFilter() {
//        FilterRegistrationBean filterReg = new FilterRegistrationBean(new IPFilter());
//        //优先级
//        filterReg.setOrder(70);
//        filterReg.setDispatcherTypes(DispatcherType.REQUEST);
//        //匹配路径
//        List<String> urlPatterns = new ArrayList<>();
//        filterReg.addUrlPatterns("/*");
//        filterReg.addInitParameter("exclusions", "/gogo,/hello");
//        filterReg.setUrlPatterns(urlPatterns);
//        System.out.println("====来了");
//        return filterReg;
//    }
//
//    public static final String PARAM_NAME_EXCLUSIONS = "exclusions";
//    private Set<String> excludesPattern;
//
//    //单位时间内最大访问数
//    private static final Integer MAX_COUNT = 15;
//    //单位时间
//    //一秒钟内访问15次
//    private static final Long UNIT_TIME = 10 * 1000L;
//    //限制时长
//    private static final Long REJECT_TIME = 10 * 60 * 1000L;
//
//    @Autowired
//    private RedisUtil redisUtil;
//
//    private ApplicationContext context;
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        HttpServletResponse response = (HttpServletResponse) servletResponse;
//        String ip = IPUtils.getIpAddr(request);
//        System.out.println("ip: " + ip);
//        //过滤黑名单
//        if (redisUtil.hasKey("filter:ip:black:" + ip)) {
//            log.error("ip访问过于频繁，已被限制=>" + ip + " 倒计时" + redisUtil.getExpire("filter:ip:black:" + ip));
//            WebUtils.issueRedirect(request, response, "/Redirect/ACCESS_RESTRICTED?REJECT_TIME=" + REJECT_TIME);
//            return;
//        }
//        //判断ip是否首次访问
//        if (redisUtil.hHasKey("filter:ip:normal", "count:" + ip)) {
//            //判断最大访问次数
//            Integer maxCount = Integer.valueOf(redisUtil.hget("filter:ip:normal", "count:" + ip).toString());
//            log.info("ip：" + ip + " 访问" + maxCount + "次");
//            if (maxCount > MAX_COUNT) {
//                Long maxTime = Long.valueOf(redisUtil.hget("filter:ip:normal", "time:" + ip).toString());
//                System.out.println(System.currentTimeMillis());
//                System.out.println(maxTime);
//                System.out.println((System.currentTimeMillis() - maxTime) / 1000);
//                if (System.currentTimeMillis() - maxTime < UNIT_TIME) {
//                    log.error("ip访问过于频繁，已被限制=>" + ip + " 倒计时" + REJECT_TIME);
//                    redisUtil.setEx("filter:ip:black:" + ip, "1", REJECT_TIME, TimeUnit.MILLISECONDS);
//                    String str[] = {"count:" + ip, "time:" + ip};
//                    redisUtil.hdel("filter:ip:normal", str);
//                }
//                initVisitsIP(ip);
//            }
//        } else {
//            initVisitsIP(ip);
//        }
//        redisUtil.hincr("filter:ip:normal", "count:" + ip, 1);
//    }
//
//    /**
//     * 初始化访问ip
//     *
//     * @param ip
//     */
//    private void initVisitsIP(String ip) {
//        redisUtil.hset("filter:ip:normal", "count:" + ip, 0);
//        redisUtil.hset("filter:ip:normal", "time:" + ip, String.valueOf(System.currentTimeMillis()));
//    }
//
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//        String param = filterConfig.getInitParameter(PARAM_NAME_EXCLUSIONS);
//        if (param != null && param.trim().length() != 0) {
//            this.excludesPattern = new HashSet(Arrays.asList(param.split("\\s*,\\s*")));
//        }
//
//    }
//
//
//
//    @Override
//    public void destroy() {
//
//    }
//}